package com.nf.health.app.activity.doctor_advice.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.doctor_advice.bean.AdviceDetail;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.MyListView;

import java.util.List;

/**
 * 医嘱详情信息列表
 */
public class DoctorAdviceByIdAdapter extends BaseAdapter {
    private Context context;
    private List<AdviceDetail.QuestionListBean> datas;
    public DoctorAdviceByIdAdapter(Context context, List<AdviceDetail.QuestionListBean> datas) {
        this.context = context;
        this.datas = datas;
    }
    @Override
    public int getCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        return datas.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View convertview, ViewGroup arg2) {
         ViewHolder holder = null;
        if (convertview==null) {
            convertview = View.inflate(context, R.layout.item_doctoradvicebyid, null);
            holder = new ViewHolder();
            holder.tv_time_item_advice_detail = (TextView) convertview.findViewById(R.id.tv_time_item_advice_detail);//医嘱titles时间
            holder.rl_item_advice_detail_close = (RelativeLayout) convertview.findViewById(R.id.rl_item_advice_detail_close);
            holder.iv_item_advice_detail = (ImageView) convertview.findViewById(R.id.iv_item_advice_detail);// 展开箭头
            holder.mlv_item_byself_ask = (MyListView) convertview.findViewById(R.id.mlv_item_byself_ask);//展开的ListView
            convertview.setTag(holder);
        } else {
            holder = (ViewHolder) convertview.getTag();
        }
        final AdviceDetail.QuestionListBean questionListBean = datas.get(position);

        holder.rl_item_advice_detail_close.setOnClickListener(new MyClickListener(holder.mlv_item_byself_ask,questionListBean));
        holder.rl_item_advice_detail_close.setOnClickListener(new MyClickListener(holder.mlv_item_byself_ask,questionListBean ));
        //设置问题
        List<AdviceDetail.QuestionListBean.QuestionBean>  subList = questionListBean.getQuestion();
        SubDoctorAdviceByIdAdapter subDoctorAdviceByIdAdapter = null;
        if(subDoctorAdviceByIdAdapter == null){
            subDoctorAdviceByIdAdapter = new SubDoctorAdviceByIdAdapter(context,subList,questionListBean.getState(),questionListBean.getId());
            holder.mlv_item_byself_ask.setAdapter(subDoctorAdviceByIdAdapter);
        }else{
            subDoctorAdviceByIdAdapter.setSubDataChanged(subList);
        }
        holder.tv_time_item_advice_detail.setText(""+ questionListBean.getSurveytime());
        return convertview;
    }
      class ViewHolder{
        TextView tv_time_item_advice_detail;
        RelativeLayout  rl_item_advice_detail_close;
        ImageView iv_item_advice_detail;
        MyListView mlv_item_byself_ask;
    }


    private OnMyClickListener listener;
    public void setOnMyClickListener(OnMyClickListener listener){
        this.listener = listener;
    }
    private void closeAllOpenedSwipeView() {
       /* for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }*/
    }
    public void setAdapterDataChanged( List<AdviceDetail.QuestionListBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }
    public class MyClickListener implements View.OnClickListener{
        private MyListView mlv_item_byself_ask;
        private AdviceDetail.QuestionListBean questionListBean;
        public MyClickListener(MyListView mlv_item_byself_ask,AdviceDetail.QuestionListBean questionListBean){
            this.mlv_item_byself_ask = mlv_item_byself_ask;
            this.questionListBean = questionListBean;
        }

        @Override
        public void onClick(View v) {
            if (questionListBean.isOpenStatuis()) {
                mlv_item_byself_ask.setVisibility(View.GONE);
                questionListBean.setOpenStatuis(false);
            } else {
                mlv_item_byself_ask.setVisibility(View.VISIBLE);
                questionListBean.setOpenStatuis(true);
            }
            if(onMyClickListener != null){
                onMyClickListener.onClick(0,"");
            }
        }
    }
    public OnMyClickListener onMyClickListener;
    public void regisitOnMyClickListener(OnMyClickListener onMyClickListener){
        this.onMyClickListener =  onMyClickListener;
    }
}
